package main;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import protocol.HTTP;
 
class MessagingServer {
    public static void main(String[] args) 
        throws NumberFormatException, IOException 
    {
      //  if (args.length != 1) {
       //     System.err.println("please supply only one argument, the port to bind.");
        //    return;
        //}
        /*The characteristic of the server concurrency model is determined by the selected implementation for the scm 
        instance (SingleThread, ThreadPerClient or ThreadPool - as described in the next sections)*/
        ServerConcurrencyModel scm = new ThreadPool(5);  
        Charset charset = Charset.forName("UTF-8");
        ServerSocket socket = new ServerSocket(9999);
        while (true) {
            Socket s = socket.accept();
            Tokenizer tokenizer = new MessageTokenizer(new InputStreamReader(s.getInputStream(),charset),'$');
            HTTP protocol = new HTTP();
            Runnable connectionHandler = new ConnectionHandler(s, charset, tokenizer, protocol);
            scm.apply(connectionHandler);
        }
        
    }
}